<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>消除游戏</title>
</head>

<body>
  <script>
    var lastRemaining = function (n) {
      if (n < 4) return n === 1 ? 1 : 2;
      return (lastRemaining(Math.floor(n / 4)) * 4) - (~n & 2)
    };
    console.log(lastRemaining(9))
  </script>
</body>

</html>